IF ELSE Statements

If else statements are the block of code which decides that the specific piece of code should be executed or not based on the value provided. It is similar like java and other languages available. We can say it is a decision maker for our logic and the part of code will only be executed if the condition is satisfied. So if and else are the decision making statement n object oriented programming which are very important when it comes to apply our logic in practice scenario. Scala provides if statement to test the conditional expressions. It tests boolean conditional expression which can be either true or false. Scala use various types of if else statements.

  • If statement
  • If-else statement
  • Nested if-else statement
  • If-else-if ladder statement
  • Scala if statement
If Statement
The scala if statement is used to test condition in scala. If block executes only when condition is true otherwise execution of if block is skipped.

Syntax:
if(condition)
{
    // Statements to be executed
}

Example
object demo
{
def main(args: Array[String]) {
var age:Int = 20
if(age > 18)
{
println ("Age is greate than 18")
}
}
}
Age is greate than 18

If else Statement
The scala if-else statement tests the condition. If the condition is true, if block executes otherwise else block executes.

Sysntax
if(condition){
    // If block statements to be executed
} else {
    // Else bock statements to be executed
}


Scala if else example
object demo
{
def main(args: Array[String]) {
var age:Int = 15
if (age <= 18) {
println("Age is less than 18")
} else {
println("Age is greate than 18")
}
}
}
object Demo {
def main(args: Array[String]) {
var number = 21
if(number%2==0)
{
println("Even number")
}
else
{
println("Odd number")
}
}
}
Odd number

Sum of the two given integer values.If the two values are the same then return triple their sum. 

object prg1 {
  def test(x:Int, y:Int) : Int =
    {
        if (x == y) (x + y) * 3 else x + y
    }
     
   def main(args: Array[String]): Unit = {
      println("Result: " + test(1, 2));
      println("Result: " + test(2, 2));
   }
}

Scala if else if Ladder Statement
The scala if-else-if ladder executes one condition among the multiple conditional statements.
if (condition1){ 
//Code to be executed if condition1 is true 
} else if (condition2){ 
//Code to be executed if condition2 is true 
} else if (condition3){ 
//Code to be executed if condition3 is true 
... 
else { 
//Code to be executed if all the conditions are false 

Example
object Demo {
def main(args: Array[String]) {
var number:Int = 85
if(number>=0 && number<50){
println ("fail")
}
else if(number>=50 && number<60){
println("D Grade")
}
else if(number>=60 && number<70){
println("C Grade")
}
else if(number>=70 && number<80){
println("B Grade")
}
else if(number>=80 && number<90){
println("A Grade")
}
else if(number>=90 && number<=100){
println("A+ Grade")
}
else println ("Invalid")
}
}

A Grade

Scala if Statement as better alternative of Ternary Operators
In scala, you can assign if statement result to a function. Scala does not have ternary operator concept like C/C++ but provides more powerful if which can return value. Let's see an example
object demo {
def main(args: Array[String]) {
val result = checkIt(-10)
println (result)
}
def checkIt (a:Int) = if (a >= 0) 1 else -1 // Passing a if expression value to function
}
-1

No comments:

Post a Comment